package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.crm;

import com.alibaba.acm.shaded.com.google.common.collect.Maps;
import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.cirtus.ots.application.adapter.ability.model.response.CallApiRecordResponse;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crpricecenter.model.dto.UpdateActivityTitleDataRequest;
import com.epoch.app.crpricecenter.model.dto.UpdateActivityTitleDataResponse;
import com.epoch.app.crpricecenter.service.CrPriceCenterService;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmCommResponse;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmCommSDO;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmIf006Request;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmIf006SDO;
import com.epoch.app.otsapplicationadaptersdk.crmv2.mncrmif006v2.service.MnCrmIf006V2Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MessageHeaderPassiveSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author hanguo
 * @date 2021/10/12
 * @Description crm请求平台同步促销活动更改（状态及有效期）
 */
@Component
public class MnCrmIf006V2ServiceImpl implements MnCrmIf006V2Service {
    private static final Log log = Log.getLogger(MnCrmIf006V2ServiceImpl.class);

    @Resource
    private CrPriceCenterService crPriceCenterService;

    private static final String MESSAGE_ID = "messageId";

    private static final String DATA_SOURCE = "dataSource";

    @ProcessInvoker(remark = "MnCrmIf006V2Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    @Override
    public MnCrmCommResponse externalSyncUpdatePromotionValidity(MnCrmIf006Request mnCrmIf006Request) {
        log.info("MnCrmIf006Request mnCrmIf006Request 入参:" +JSONObject.toJSONString(mnCrmIf006Request));

        MnCrmCommResponse mnCrmCommResponse = new MnCrmCommResponse();
        mnCrmCommResponse.setMessageId(mnCrmIf006Request.getMessageHeader().getMessageId());
        mnCrmCommResponse.setInterfacePath(mnCrmIf006Request.getMessageHeader().getInterfacePath());
        MnCrmCommSDO mnCrmCommSDO = new MnCrmCommSDO();

        //数据来源记录
        MessageHeaderPassiveSDO messageHeader = mnCrmIf006Request.getMessageHeader();
        Map<String,String> features = Maps.newHashMap();
        if(Objects.nonNull(messageHeader)){
            //数据头信息id
            features.put(MESSAGE_ID,messageHeader.getMessageId());
            //数据来源 sap或者excel等
            features.put(DATA_SOURCE, messageHeader.getSender());
        }

        CallApiRecordResponse callApiRecordResponse = new CallApiRecordResponse();

        if (Objects.nonNull(mnCrmIf006Request.getData())) {
            MnCrmIf006SDO data = mnCrmIf006Request.getData();
            UpdateActivityTitleDataRequest updateActivityTitleDataRequest = new UpdateActivityTitleDataRequest();

            updateActivityTitleDataRequest.setActivityId(data.getActivityId());
            updateActivityTitleDataRequest.setActivityStartDate(SapAndCrmFieldDealUtils.toDate(data.getOrderStartDate()));
            updateActivityTitleDataRequest.setActivityEndDate(SapAndCrmFieldDealUtils.toDate(data.getOrderEndDate()));
            updateActivityTitleDataRequest.setOrderStartDate(SapAndCrmFieldDealUtils.toDate(data.getOrderStartDate()));
            updateActivityTitleDataRequest.setOrderEndDate(SapAndCrmFieldDealUtils.toDate(data.getOrderEndDate()));
            updateActivityTitleDataRequest.setActivityStatus(data.getActivityStatus());
            updateActivityTitleDataRequest.setStatusRemark(data.getStatusRemarks());
            updateActivityTitleDataRequest.setFeatures(JSONObject.toJSONString(features));

            UpdateActivityTitleDataResponse updateActivityTitleDataResponse = crPriceCenterService.updateActivityTitleData(updateActivityTitleDataRequest);
            log.info("促销活动更改有效期 result = {}", JSONObject.toJSONString(updateActivityTitleDataResponse));
            if (!updateActivityTitleDataResponse.getSuccess()) {
                mnCrmCommSDO.setResultType(MnSAPStatus.ERROR.getCode());
                mnCrmCommSDO.setResultMessage(updateActivityTitleDataResponse.getMessage());
            }

            callApiRecordResponse.setRequestBody(JSONObject.toJSONString(updateActivityTitleDataRequest));
            callApiRecordResponse.setOperateResult(updateActivityTitleDataResponse.getSuccess());
            callApiRecordResponse.setTableName("ofs_activity_title");
            callApiRecordResponse.setMessage(updateActivityTitleDataResponse.getMessage());
        }else{
            mnCrmCommSDO.setResultType(MnSAPStatus.ERROR.getCode());
            mnCrmCommSDO.setResultMessage("请传数据!");
        }
        //如果没有执行出错的记录
        if (StringUtils.isBlank(mnCrmCommSDO.getResultType())) {
            mnCrmCommSDO.setResultType(MnSAPStatus.SUCCESS.getCode());
        }
        if (StringUtils.isBlank(mnCrmCommSDO.getResultMessage())) {
            mnCrmCommSDO.setResultMessage("同步成功");
        }

        log.info("MnCrmIf006ServiceImpl_externalSyncUpdatePromotionValidity=>执行结束修改结果 = {}",JSONObject.toJSONString(callApiRecordResponse));

        mnCrmCommResponse.setData(mnCrmCommSDO);
        return mnCrmCommResponse;
    }
}
